var gamerslist=[];
var gamerslist_temp=[];
var gamersdata=[];
var gamersdata_temp=[];
var deathlist=[];
var temp=Math.random()*100;
var _this=this;
exports.game=function(io){
    io.on('connection', function(socket){
        // console.log('已建立连接'+socket.handshake.headers.origin);
        // socket.emit('news',{message:'hello'});
        socket.on('join',function(data){
            // console.log(data);
            _this.joingame(socket,io);
        });
        socket.on('live',function (data) {
            var id=socket.conn.id;
            if(data.check===temp&&gamerslist_temp.indexOf(id)===-1) {
                gamerslist_temp.push(id);
                if(data.sync){
                    gamersdata_temp.push({id:id,data:data.sync})
                }
                // console.log(id+'存活');
            }
        });
        // socket.on('upload',function(data){
        //     _this.sync_up({
        //         id:socket.conn.id,
        //         data:data
        //     });
        // })
    });
};
var getColor=function(){
    return  '#' +
        (function(color){
            return (color +=  '0123456789abcdef'[Math.floor(Math.random()*16)])
            && (color.length === 6) ?  color : arguments.callee(color);
        })('');
};
exports.joingame=function(socket,io){
    var id=socket.conn.id;
    _this.checkgamers(io);
    if(gamerslist.indexOf(id)===-1){
        // gamers++;
        gamerslist.push(id);
    }
    socket.emit('accept',{number:gamerslist.length,id:id,color:getColor()});
    console.log(id+'加入游戏');
};

exports.checkgamers=function(io){
    temp=Math.random()*100;
    var oldgamerslist=gamerslist.slice(0),
        oldgamersdata=gamersdata.slice(0);
    gamerslist=gamerslist_temp;
    for(item in deathlist){
        if(deathlist[item]){
            // console.log(deathlist[item]);
            var index=gamerslist.indexOf(deathlist[item].id)
            if(index!==-1){
                deathlist=deathlist.slice(index);
            }
        }

    }
    gamersdata=gamersdata_temp;
    for(var item in oldgamerslist){
        if(gamerslist_temp.indexOf(oldgamerslist[item])===-1){
            var f=false;
            // console.log(oldgamerslist[item]+'暂时失去连接');
            for(var item2 in deathlist){
                // console.log(deathlist[item2].id);
                if(deathlist[item2].id===oldgamerslist[item]){
                    f=true;
                    // console.log(oldgamerslist[item]+'失联时长'+deathlist[item2].num);
                    if(++deathlist[item2].num<10){
                        gamerslist.push(oldgamerslist[item]);
                        for(var item3 in oldgamersdata){
                            if(oldgamersdata[item3].id===oldgamerslist[item]){
                                gamersdata.push(oldgamersdata[item3]);
                                break;
                            }
                        }
                    }else{
                        console.log(oldgamerslist[item]+'永久失去连接');
                        deathlist=deathlist.slice(item2);
                    }
                    break;
                }
            }
            if(!f){
                deathlist.push({id:oldgamerslist[item],num:0});
                // console.log(oldgamerslist[item]+'使用历史数据弥补丢包');
                gamerslist.push(oldgamerslist[item]);
                for(var item3 in oldgamersdata){
                    if(oldgamersdata[item3].id===oldgamerslist[item]){
                        gamersdata.push(oldgamersdata[item3]);
                        break;
                    }
                }
            }
        }
    }

    gamerslist_temp=[];
    gamersdata_temp=[];
    io.sockets.emit('check',{'check':temp,'number':gamerslist.length,'sync':gamersdata})
};
// exports.sync_up=function(arg){
//     console.log(data);
//
// };
// exports.sync_down=function(){
//     io.socket.emit
// }